package com.ruoyi.project.tj.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.custom.domain.CustomInfo;
import com.ruoyi.project.custom.service.ICustomInfoService;
import com.ruoyi.project.tj.domain.TjOperationSummary;
import com.ruoyi.project.tj.service.ITjOperationSummaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/tj/operation")
public class TjOperationSummaryController extends BaseController {
    @Autowired
    private ITjOperationSummaryService operationService;
    @Autowired
    private ICustomInfoService customService;

    @GetMapping("/list")
    public TableDataInfo list(TjOperationSummary summary)
    {
        startPage();
        List<TjOperationSummary> list = operationService.selectOperationSummary(summary);
        return getDataTable(list);
    }

    @GetMapping("/selectOperationSummaryById/{id}")
    public AjaxResult selectOperationSummaryById(@PathVariable("id") String id)
    {
        return AjaxResult.success(operationService.selectOperationSummaryById(id));
    }

    @GetMapping("/init/{month}")
    public AjaxResult init(@PathVariable("month") String month)
    {
        Date operEndDate = DateUtils.parseDate(month + "-01");
        Date operStartDate = DateUtils.addDays(DateUtils.addMonths(operEndDate, 1), -1);

        List<CustomInfo> list = customService.selectOperationSummaryInitData(operStartDate, operEndDate);
        if(list != null && list.size() > 0){
            for(CustomInfo c : list) {
                TjOperationSummary operationSummary = new TjOperationSummary();
                operationSummary.setMonth(month.replace("-", ""));
                operationSummary.setCustomId(c.getId());
                operationService.insertOperationSummary(operationSummary);
            }
        }

        return AjaxResult.success();
    }

    @PostMapping("")
    public AjaxResult insert(@RequestBody TjOperationSummary summary)
    {
        return toAjax(operationService.insertOperationSummary(summary));
    }

    @PutMapping("")
    public AjaxResult update(@RequestBody TjOperationSummary summary)
    {
        return toAjax(operationService.updateOperationSummary(summary));
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") String id)
    {
        return toAjax(operationService.deleteOperationSummary(id));
    }
}
